'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async classifyList(){
    const { ctx, app } = this;
    const data=await app.mysql.select("labelss")
    let newobj={}
    //过滤数据 筛选出分类名称不同的对象
    const a=data.reduce((pre,cur)=>{
        newobj[cur.tag]?"":newobj[cur.tag]=pre.push(cur)
        return pre
    },[])
    console.log(a);
    ctx.body={
      code:200,
      data:a
    }
  }
  async addClassify(){
    const {ctx,app}=this
    const {name,value}=ctx.request.body
    const result=await app.mysql.get("labelss",{tag:name,measure:value})
    if(result){
      ctx.body={
        code:504,
        mess:"数据存在，请修改"
      }
    }else{
      let data=await app.mysql.insert("labelss",{
        tag:name,
        measure:value,
        id:null
      })
      if(data.affectedRows){
        ctx.body={
          code:200,
          data:await app.mysql.select("labelss")
        }
      }
    }
  }
  async findClassify(){
    const {ctx,app}=this
    const {id}=ctx.request.body
    const data=await app.mysql.get("labelss",{id})
    if(data){
      ctx.body={
        code:200,
        data:data
      }
    }
  }
}

module.exports = HomeController;
